package com.kzd.btgame.main.mine

import android.webkit.*
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.custom.setTransitionStatus
import com.kzd.btgame.main.*
import com.kzd.btgame.main.web.WebViewActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.ownerViewModel

class MineRichTextActivity : WebViewActivity() {

    private val mUrl: String?
        get() = intent?.extras?.getString(KEY_RICH_TEXT_URL)

    private val mContent: String?
        get() = intent?.extras?.getString(KEY_RICH_TEXT_CONTENT)

    private val mType: String?
        get() = intent?.extras?.getString(KEY_RICH_TEXT_TYPE)


    private val mViewModel:BaseViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override fun initData() {
        mContent?.let { mWebView.loadDataWithBaseURL(it) }
        mType?.let { mViewModel.userAgreement(it) }
        mUrl?.let { mWebView.loadUrl(it) }
    }

    override fun initViewMode() {
        mViewModel.userAgreementLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            if (!data.url.isNullOrEmpty()) {
                mWebView.loadUrl(data.url)
            } else if (!data.content.isNullOrEmpty()) {
                mWebView.loadDataWithBaseURL(data.content)
            }
        })
    }

    override fun onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack()
        } else {
            super.onBackPressed()
        }
    }
}